home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / FDF101.ARJ / CMP.C next >
C/C++ Source or Header  |  1992-04-29  |  3KB  |  144 lines

  1. /*
  2.  * cmp.c
  3.  *
  4.  * module to allocate memory and compare two files.
  5.  *
  6.  * Roy Bixler
  7.  * March 15, 1991
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 1, or (at your option)
  12.  * any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23.  
  24.  
  25.  
  26. #include <fcntl.h>
  27. #include <\sys\types.h>    /* must be included before stat.h */
  28. #include <\sys\stat.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31.  
  32. #include "cmp.h"
  33.  
  34.  
  35.  
  36. #define CMP_FILE_BUF_SIZE 0xfffe
  37.  
  38.  
  39.  
  40. /*
  41.  * alloc_file_buffers
  42.  *
  43.  * allocate two file buffers in preparation for a file compare.  The
  44.  * size of the two buffers starts at 64K or the file size (whichever is
  45.  * smaller) and works its way down.  Returns zero on total failure and the
  46.  * allocated buffer size otherwise.
  47.  */
  48. unsigned int alloc_file_buffers(char **buf1, char **buf2, long file_size)
  49.  
  50. {
  51.     unsigned int buf_size = (unsigned int) ((file_size <=
  52.                                             ((long) CMP_FILE_BUF_SIZE))
  53.                                            ? file_size
  54.                                            : CMP_FILE_BUF_SIZE);
  55.  
  56.     *buf1 = *buf2 = NULL;
  57.     while ((((*buf1 = malloc((size_t) buf_size)) == NULL) ||
  58.             ((*buf2 = malloc((size_t) buf_size)) == NULL)) &&
  59.            (buf_size > 0)) {
  60.         if (*buf1 != NULL)
  61.             free(*buf1);
  62.         if (*buf2 != NULL)
  63.             free(*buf2);
  64.         buf_size >>= 1;
  65.     }
  66.  
  67.     return buf_size;
  68. }
  69.  
  70.  
  71.  
  72. /*
  73.  * do_compare_files
  74.  *
  75.  * given handles of 2 files opened for read, compare the contents of the two
  76.  * and return non-zero if they are the same, zero if not.
  77.  */
  78. int do_compare_files(int file1, int file2)
  79.  
  80. {
  81.     struct stat tmp1, tmp2;
  82.     char *buf1, *buf2;
  83.     int ret_val = 1;
  84.     unsigned int buf_len;
  85.     long i, read1, read2, file_len;
  86.  
  87.     fstat(file1, &tmp1);
  88.     fstat(file2, &tmp2);
  89.     if ((file_len = tmp1.st_size) != tmp2.st_size)
  90.         return 0;
  91.     else if (!file_len)
  92.         return 1;
  93.     else if (!(buf_len = alloc_file_buffers(&buf1, &buf2, file_len))) {
  94.         printf("could not allocate compare file buffers - file length = %ld\n", file_len);
  95.         exit(-1);
  96.     }
  97.  
  98.     for (i=0; i<file_len; i += buf_len) {
  99.         read1 = read(file1, buf1, (long) buf_len);
  100.         read2 = read(file2, buf2, (long) buf_len);
  101.         if ((read1 != read2) || (memcmp(buf1, buf2, (size_t) buf_len))) {
  102.             ret_val = 0;
  103.             break;
  104.         }
  105.     }
  106.  
  107.     free(buf1);
  108.     free(buf2);
  109.  
  110.     return ret_val;
  111. }
  112.  
  113.  
  114.  
  115. /*
  116.  * compare_files
  117.  *
  118.  * given a pair of files specified by path and name, return non-zero if their
  119.  * contents match, zero if contents don't match.
  120.  *
  121.  * Note: If both files can't be opened, it reports they are the same
  122.  * (i.e. non-existent)!
  123.  */
  124. int compare_files(char *file1, char *file2)
  125.  
  126. {
  127.     int ret_val, in1, in2;
  128.  
  129.     if (((in1 = open(file1, O_RDONLY, 0)) < 0) ||
  130.         ((in2 = open(file2, O_RDONLY, 0)) < 0)) {
  131.         printf("file compare failed to open both files\n");
  132.         if (in1 >= 0)
  133.             close(in1);
  134.         ret_val = ((in1 < 0) && (in2 < 0));
  135.     }
  136.     else {
  137.         ret_val = do_compare_files(in1, in2);
  138.         close(in1);
  139.         close(in2);
  140.     }
  141.  
  142.     return ret_val;
  143. }
  144.